package com.neusoft.elmboot.controller;

import com.neusoft.elmboot.po.Business;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.neusoft.elmboot.po.Merchant;
import com.neusoft.elmboot.service.MerchantService;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.HashMap;

@RestController
@RequestMapping("/MerchantController")
public class MerchantController {
	
	@Autowired
	private MerchantService merchantService;

	@PostMapping("/checkMerchantStatus")
	public Map<String, Object> checkMerchantStatus(@RequestParam String userId) {
		Merchant merchant = merchantService.checkMerchantStatus(userId);
		Map<String, Object> response = new HashMap<>();

		// 如果商家信息不为空，则商家已注册
		if (merchant != null) {
			response.put("isMerchant", true);
			response.put("businessId", merchant.getBusinessId());  // 返回商家ID
		} else {
			response.put("isMerchant", false);
		}
		return response;
	}

	@RequestMapping("/register")
	public ResponseEntity<String> register(
			@RequestParam String businessName,
			@RequestParam String businessAddress,
			@RequestParam String businessExplain,
			@RequestParam BigDecimal starPrice,
			@RequestParam BigDecimal deliveryPrice,
			@RequestParam(required = false) String remarks,
			@RequestParam String userId,
			@RequestParam(required = false) MultipartFile businessImg) {  // 可选的图片文件
		try {
			Business business = new Business();
			business.setBusinessName(businessName);
			business.setBusinessAddress(businessAddress);
			business.setBusinessExplain(businessExplain);
			business.setOrderTypeId(1);
			business.setStarPrice(starPrice);
			business.setDeliveryPrice(deliveryPrice);
			business.setRemarks(remarks);
			business.setUserId(userId);

			// 如果有上传的图片，则保存图片
			if (businessImg != null && !businessImg.isEmpty()) {
				String imgPath = saveImage(businessImg);  // 假设你有方法保存图片并返回路径
				business.setBusinessImg(imgPath);
			} else {
				business.setBusinessImg("");  // 没有图片时存为空
			}

			int result = merchantService.saveBusiness(business);
			if (result > 0) {
				return ResponseEntity.ok("商家注册成功");
			} else {
				return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("商家注册失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("注册过程中发生错误: " + e.getMessage());
		}
	}

	// 新增保存图片的逻辑
	public String saveImage(MultipartFile file) throws IOException {
		// 构建保存图片的路径，假设图片存储在 src/assets 目录下
		String fileName = System.currentTimeMillis() + "_" + file.getOriginalFilename();
		String uploadDir = "/www/wwwroot/http/image/";
		Path path = Paths.get(uploadDir + fileName);

		// 确保目录存在
		File directory = new File(uploadDir);
		if (!directory.exists()) {
			directory.mkdirs();  // 创建目录
		}

		// 写入文件
		Files.write(path, file.getBytes());

		return path.toString();  // 返回图片保存路径
	}
}
